package com.youzan.cloud.biz.integral.usercase;
import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.biz.integral.common.UserCase;
import com.youzan.cloud.biz.integral.common.UserCommand;
import com.youzan.cloud.biz.integral.dal.mapper.PointChangeMapper;
import com.youzan.cloud.biz.integral.dal.mapper.UserPointMapper;
import com.youzan.cloud.biz.integral.extension.LinkerExtensionPoints;
import com.youzan.cloud.biz.integral.extension.PointLinkerConfigurator;
import com.youzan.cloud.connect.service.common.domain.BizServiceInfo;
import com.youzan.cloud.connect.service.sdk.ConnectServiceCaller;
import com.youzan.cloud.connect.service.sdk.ConnectServiceResponse;
import com.youzan.cloud.connect.service.sdk.exception.ConnectServiceCallException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @author hlin
 */
public abstract class AbstractPointUserCase<T extends UserCommand> implements UserCase<T> {

    private static final Logger log = LoggerFactory.getLogger(AbstractPointUserCase.class);

    @Autowired
    protected PointChangeMapper pointChangeMapper;

    @Autowired
    protected UserPointMapper userPointMapper;

    @Autowired
    protected ConnectServiceCaller connectServiceCaller;

    @Autowired
    protected PointLinkerConfigurator linkerConfigurator;


    protected ConnectServiceResponse<?> callLinker(LinkerExtensionPoints point, Object request) throws ConnectServiceCallException {

        BizServiceInfo linkerInfo = linkerConfigurator.getBizServiceInfoMap().get(point.name());
        if (linkerInfo == null) {
            log.warn("call linker failed, not linker matched for this the point {}", JSONObject.toJSONString(point));
            return null;
        }

        // call connector
        return (ConnectServiceResponse<?>) connectServiceCaller
                .callConnectService(linkerInfo.getConnectServiceName(), linkerInfo.getTargetSystem(), linkerInfo.getTargetSystemVersion(), request);
    }
}
